/*
 * kalib_con.c
 *
 * Created: 2012-08-22 21:55:32
 *  Author: Antrykot
 */ 

#include "../sc-s/main.h"
#include "../sc-s/lcd.h"
#include "../sc-s/sc-s.h"
#include "../sc-s/utils.h"
#include <stdlib.h>
#include "../sc-s/trans-s.h"


void kalib_routin()
{
	
	if (calib_ptr < CM_STNIS)
	{
		calib_ptr = CM_STNIS;
	}
	
	u16 time;
	lcd_home();
	time = TimerH * 64 + (TCNT1 / 1000);
	
	if (!PLU || !MIN || !ENT || !last_time )
	{
		last_time = time;
	}

	if (time - last_time > 180 || time > 480)		// 60s/ 8min time out.
	{
		tp_addr = P_ADDR_RESET_DEFAULTS;
		tk_data = 1;
		tk_addr = K_ADDR_CAL_FINISH;		// finish error
		tk_repeat = 3;
		lcd_print(" !CZAS! ");
		WAITMS(1000);
		lcd_home();
		lcd_print("!KONIEC!");
		WAITMS(3000);
		Pro = PRO_WORK;
		Awp = _read(EEP_AWP);
		return;
	}

	switch (calib_ptr)
	{
		
		
		case CM_STNIS:
		if (calib_ptr_deep)
		{
			cal_low();
			return;
		}
		lcd_print(" KALIB. ");
		if (!ENT)
		{
			calib_ptr_deep = 1;
			//tp_addr = P_ADDR_SW_LRANGE;
			//wait_deliver_P();
			WAITMS(200);
			return;
		}
		break;
		
		
		case CM_FAB:

		lcd_print("UST.FAB ");
		if (!ENT)
		{
			tp_addr = P_ADDR_L_RESET;
			lcd_home();
			lcd_print("ZAPIS...");
			wait_deliver_P();
			//tp_addr = P_ADDR_H_RESET;
			//wait_deliver_P();
			
			calib_ptr++;
			WAITMS(400);

			lcd_home();
			lcd_print("ZAPISANO");

			WAITMS(2000);
			return;
		}
		break;
		
		case CM_WYJ:
		if (calib_ptr_deep)
		{
			cal_out();
			return;
		}

		lcd_print("WYJSCIE ");
		if (!ENT)
		{
			calib_ptr_deep = 1;
			while(!ENT);
			return;
		}
		break;
	}
	
	
	if (!PLU)
	calib_ptr++;
	if (!MIN)
	calib_ptr--;
	
	if (calib_ptr > CM_WYJ)
	calib_ptr = CM_STNIS;
	
	if (calib_ptr < CM_STNIS)
	{
		calib_ptr = CM_WYJ;
	}
	while((!PLU) || (!MIN) || (!ENT));
	
}


#ifdef MEDIA_CON
#define SZ_PLUS	50
#define SZ_MINUS	50
#define SC_PLUS	50
#define SC_MINS	50

#define GAS_MAX	190
#define GAS_MIN	140

#elif MEDIA_COS
#define SZ_PLUS	100
#define SZ_MINUS	80
#define SC_PLUS	100
#define SC_MINS	100

#define GAS_MAX	950
#define GAS_MIN	680

#endif

#ifndef MEDIA_O2

void cal_low()
{
	char str[8];
	lcd_home();

	switch (calib_ptr_deep)
	{
		
		case 0x01:
		{
			char s[8];
			if (v_tmp & 0x8000)
			{
				lcd_print("T:-");
			}
			else
			{
				lcd_print("T: ");
			}
			fuse1(s,v_tmp & 0x7FFF);
			lcd_print(s);
			lcd_print("  ");
			WAITMS(1000);
			break;
		}
		case 0x02:
		lcd_print(" POD.POW");
		wait_deliver_P();
		tp_addr = P_ADDR_L_RESET;
		wait_deliver_P();
		WAITMS(1500);
		
		calib_ptr_deep++;
		break;
		case 0x03:

		show_media(meas,0x80);		// show spec
		if (!ENT)
		{
			if (meas & 0x1000)
			{
				if ((meas & 0x0FFF) > SZ_MINUS)	// -50ppm
				{
					kalib_serwis(0,(1<<14));
					// TODO : Write serwis Z to K
					return;
				} 
			}
			else
			{
				if ((meas & 0x0FFF) > SZ_PLUS)	// +50ppm
				{
					kalib_serwis(0,(1<<14));
					// TODO : Write serwis Z to K
					return;
				}
				
			}
			p_ack = 0;
			tp_addr = P_ADDR_CALLZ;
			
			tk_data = (1 << 14);
			tk_addr = K_ADDR_LZ;
			tk_repeat = 3;
			// TODO : WRITE ZERO IN K
			
			calib_ptr_deep++;
			lcd_home();
			lcd_print("UST 0ppm");
			wait_deliver_P();
			WAITMS(1000);
			lcd_home();
			lcd_print(" ZAPIS! ");
			WAITMS(1000);
		}
		break;
		case 0x04:
		lcd_print(" UST.GAZ");
		WAITMS(1000);
		calib_ptr_deep++;
		
		calib_gas = read_word(EEP_STN);
		break;
		
		case 0x05:
		itoa(calib_gas,str,10);
		lcd_print("GAZ:");
		lcd_print(str);
		lcd_print(" ");
		
		if (!PLU)
		{
			calib_gas++;
			if (calib_gas > GAS_MAX)
			calib_gas = GAS_MAX;
		}
		
		if (!MIN)
		{
			calib_gas--;
			if (calib_gas < GAS_MIN)
			calib_gas = GAS_MIN;
		}
		
		if (!ENT)
		{
			calib_ptr_deep++;
			lcd_home();
			lcd_print("POD.GAZ.");
			
			write_word(EEP_STN,calib_gas);
			
			// TODO : WRITE GAS TO P & K
			
			
			WAITMS(1000);
		}
		break;
		
		case 0x06:
		
		show_media(meas,0x80);
		

		
		if (!ENT)
		{
			if (meas & 0x1000)	// 12bit
			{
				kalib_serwis(1,calib_gas | (1 << 14));
				return;
			}
			else
			{
				if (((meas & 0x0FFF) < calib_gas - SC_PLUS) || ((meas & 0x0FFF) > calib_gas + SC_MINS))
				{
					kalib_serwis(1,calib_gas | (1 << 14));
					return;
				}
			}
			tp_data = calib_gas;
			tp_addr = P_ADDR_CALLS;
			
			tk_data = calib_gas | (1 << 14);
			tk_addr = K_ADDR_LS;
			tk_repeat = 3;

			
			lcd_home();
			lcd_print("UST ");
			itoa(calib_gas,str,10);
			lcd_print(str);
			lcd_print(" ");
			WAITMS(2000);
			calib_ptr_deep = 0;
			calib_ptr = CM_WYJ;

		}
		
	}
	
	
}
#endif
